//
//  GQHFreightInfoView.m
//  Xanthium
//
//  Created by GuanQinghao on 08/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHFreightInfoView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"

@interface GQHFreightInfoView () <UITableViewDelegate,UITableViewDataSource,GQHFollowedCellDelegate> {
    
    NSArray *titleArray;
}

@end


@implementation GQHFreightInfoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    titleArray = @[@"起始地",@"目的地",@"估算里程",@"货物信息",@"货物单价",@"需求车型",@"需求车长",@"装车时间",@"备注"];
//    _qh_contentArray = @[@"陕西 西安",@"云南 昆明",@"12304",@"洛川红富士🍎",@"农产品",@"50",@"100",@"1000",@"10.00",@"厢式货车",@"17.50",@"2018-03-16 09:30"];
}

#pragma mark --View
- (void)loadUserInterface {
    
    UIButton *mapButton = [UIButton buttonWithType:UIButtonTypeCustom];
    mapButton.backgroundColor = kAppYellowColor;
    [mapButton setTitle:@"查看地图" forState:UIControlStateNormal];
    [mapButton addTarget:self action:@selector(checkMap:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:mapButton];
    [mapButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self);
        make.width.mas_equalTo(self.mas_width).with.multipliedBy(0.33);
        make.bottom.mas_equalTo(self).with.inset(self.qh_homeIndicatorHeight);
        make.height.mas_equalTo(50);
    }];
    
    UIButton *dialButton = [UIButton buttonWithType:UIButtonTypeCustom];
    dialButton.backgroundColor = kAppOrangeColor;
    [dialButton setTitle:@"联系货主" forState:UIControlStateNormal];
    [dialButton addTarget:self action:@selector(dialPhoneNumber:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:dialButton];
    [dialButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(mapButton.mas_right);
        make.bottom.and.width.and.height.mas_equalTo(mapButton);
    }];
    
    _qh_quoteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _qh_quoteButton.backgroundColor = kAppPinkColor;
    [_qh_quoteButton addTarget:self action:@selector(quotePrice:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_qh_quoteButton];
    [_qh_quoteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(dialButton.mas_right);
        make.bottom.and.height.mas_equalTo(mapButton);
        make.right.mas_equalTo(self);
    }];
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(mapButton.mas_top);
    }];
}

#pragma mark --Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (1 == section) {
        
        return titleArray.count;
    }
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (1 == indexPath.section) {
        
        return 50.0f;
    }
    
    return 80.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (0 == indexPath.section) {
        
        static NSString *identifier = @"aCell";
        GQHFollowedCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[GQHFollowedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.qh_delegate = self;
        
        /// 头像
        NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:_qh_avatar];
        NSURL *URL = [NSURL URLWithString:URLString];
        [cell.qh_avatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
        cell.qh_nameLabel.text = _qh_name;
        cell.qh_subtitleLabel.text = _qh_score;
        cell.qh_contentLabel.text = _qh_orgName;
        cell.qh_followed = _qh_followed;
        
        return cell;
    } else {
        
        static NSString *identifier = @"bCell";
        GQHCustomTextDoubleHorizontalCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[GQHCustomTextDoubleHorizontalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.qh_leftLabel.text = titleArray[indexPath.row];
        cell.qh_rightLabel.text = _qh_contentArray[indexPath.row];
        
        /// 重新布局
        [cell.qh_leftLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_equalTo(cell.contentView.mas_centerX).with.inset(50);
            
        }];
        [cell.qh_rightLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(cell.contentView.mas_centerX).with.inset(-50);
        }];
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (1 == section) {
        
        return 5.0f;
    }
    
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = kLightGrayColor;
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

#pragma mark --TargetMethod
- (IBAction)checkMap:(id)sender {
    DLog(@"查看地图");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkMap)]) {
        
        [_qh_delegate qh_checkMap];
    }
}

- (IBAction)dialPhoneNumber:(id)sender {
    DLog(@"联系货主");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_dialPhoneNumber)]) {
        
        [_qh_delegate qh_dialPhoneNumber];
    }
}

- (IBAction)quotePrice:(id)sender {
    DLog(@"我要报价");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_quotePrice)]) {
        
        [_qh_delegate qh_quotePrice];
    }
}

- (void)qh_followedOrUnfollowed:(UIButton *)sender {
    
    if ([_qh_delegate respondsToSelector:@selector(qh_followedOrUnfollowed:)]) {
        
        [_qh_delegate qh_followedOrUnfollowed:sender];
    }
}

@end
